🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

acorn

Package Overview
Dependencies
Maintainers
0
Versions
136
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

acorn

ECMAScript parser

8.14.1
latest
Source
npm
Version published
Weekly downloads
114M
12.93%
Maintainers
0
Weekly downloads
 
Created

What is acorn?

Acorn is a fast, small, and standards-compliant JavaScript parser written in ECMAScript. It is used to parse JavaScript code and generate abstract syntax trees (ASTs), which can be analyzed or transformed by other tools. It is commonly used in the development of JavaScript compilers, static analysis tools, and code transformation utilities.

What are acorn's main functionalities?

Parsing JavaScript

This feature allows you to parse a string of JavaScript code into an abstract syntax tree (AST). The 'ecmaVersion' option specifies the ECMAScript version to parse.

const acorn = require('acorn');
const AST = acorn.parse('const x = 1;', { ecmaVersion: 2020 });

Walking the AST

This feature involves traversing the generated AST to visit different types of nodes. The 'acorn-walk' package provides utilities for walking the AST.

const acorn = require('acorn');
const walk = require('acorn-walk');
const AST = acorn.parse('const x = 1;', { ecmaVersion: 2020 });
walk.simple(AST, {
  Literal(node) {
    console.log(`Found a literal: ${node.value}`);
  }
});

Dynamic Import Parsing

This feature allows acorn to handle dynamic imports in the code. The 'acorn-dynamic-import' plugin extends acorn's capabilities to parse dynamic import() expressions.

const acorn = require('acorn');
const dynamicImport = require('acorn-dynamic-import').default;
const parser = acorn.Parser.extend(dynamicImport);
const AST = parser.parse('import("./module.js");', { ecmaVersion: 2020 });

Other packages similar to acorn

FAQs

Package last updated on 05 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts